/*
 * Created by JFormDesigner on Tue May 28 14:48:14 CST 2019
 */

package com.n9.buymsg.view;


import com.n9.buymsg.event.LoginEvent;
import com.n9.buymsg.event.LoginFormInitEvent;
import org.apache.commons.lang3.StringUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;

/**
 * @author Brainrain
 */
public class LoginForm extends EventComponent {

    public LoginForm() {
        initComponents();

        publishEvent(new LoginFormInitEvent(this));
    }

    private String getAccount() {
        return  this.tUsername.getText().trim();
    }

    private String getPassword() {
        return this.tPassword.getText().trim();
    }

    private void loginBtnActionPerformed(ActionEvent e) {
        String account = getAccount();
        String password = getPassword();

        if (StringUtils.isBlank(account)) {
            DialogUtils.showError(this, "登陆错误", "账号不能为空");
            return;
        }
        if (StringUtils.isBlank(password)) {
            DialogUtils.showError(this, "登陆错误", "密码不能为空");
            return;
        }

//        LoginEvent.LoginCallback callback = (result, msg) -> {
//            if (result) {
//                this.setVisible(false);
//                MainForm.getInstance().setVisible(true);
//            } else {
//
//            }
//        };

        LoginEvent loginEvent = new LoginEvent(this);
        loginEvent.setAccount(account);
        loginEvent.setPassword(password);

        publishEvent(loginEvent);
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        panel1 = new JPanel();
        panel5 = new JPanel();
        panel2 = new JPanel();
        label2 = new JLabel();
        tUsername = new JTextField();
        panel3 = new JPanel();
        label3 = new JLabel();
        tPassword = new JTextField();
        panel4 = new JPanel();
        loginBtn = new JButton();

        //======== this ========
        setTitle("\u7528\u6237\u767b\u9646");
        setMinimumSize(new Dimension(500, 300));
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setAlwaysOnTop(true);
        Container contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());

        //======== panel1 ========
        {
            panel1.setLayout(new FlowLayout());

            //======== panel5 ========
            {
                panel5.setPreferredSize(new Dimension(400, 35));
                panel5.setLayout(new FlowLayout());
            }
            panel1.add(panel5);

            //======== panel2 ========
            {
                panel2.setPreferredSize(new Dimension(400, 60));
                panel2.setLayout(new FlowLayout(FlowLayout.LEFT));

                //---- label2 ----
                label2.setText("\u7528\u6237\u540d\uff1a");
                label2.setPreferredSize(new Dimension(60, 45));
                panel2.add(label2);

                //---- tUsername ----
                tUsername.setPreferredSize(new Dimension(250, 45));
                panel2.add(tUsername);
            }
            panel1.add(panel2);

            //======== panel3 ========
            {
                panel3.setPreferredSize(new Dimension(400, 60));
                panel3.setLayout(new FlowLayout(FlowLayout.LEFT));

                //---- label3 ----
                label3.setText("\u5bc6  \u7801\uff1a");
                label3.setPreferredSize(new Dimension(60, 45));
                panel3.add(label3);

                //---- tPassword ----
                tPassword.setPreferredSize(new Dimension(250, 45));
                panel3.add(tPassword);
            }
            panel1.add(panel3);

            //======== panel4 ========
            {
                panel4.setPreferredSize(new Dimension(400, 60));
                panel4.setLayout(new FlowLayout());

                //---- loginBtn ----
                loginBtn.setText("\u767b\u9646");
                loginBtn.setPreferredSize(new Dimension(120, 45));
                loginBtn.addActionListener(e -> loginBtnActionPerformed(e));
                panel4.add(loginBtn);
            }
            panel1.add(panel4);
        }
        contentPane.add(panel1, BorderLayout.CENTER);
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
        this.setSize(500, 320);
        this.setLocationRelativeTo(null);
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JPanel panel1;
    private JPanel panel5;
    private JPanel panel2;
    private JLabel label2;
    private JTextField tUsername;
    private JPanel panel3;
    private JLabel label3;
    private JTextField tPassword;
    private JPanel panel4;
    private JButton loginBtn;
    // JFormDesigner - End of variables declaration  //GEN-END:variables

    public void setAccountAndPassword(String account, String password) {
        SwingUtilities.invokeLater(()-> {
            tUsername.setText(account);
            tPassword.setText(password);
        });
    }
}
